package com.baidu.infinity.ui.view

import android.content.Context
import android.graphics.ColorSpace.Model
import android.util.AttributeSet
import android.widget.LinearLayout
import com.baidu.infinity.model.IconModel
import com.baidu.infinity.ui.util.OperationType
import com.baidu.infinity.ui.util.dp2px

class IconMenuView(
    context: Context,
    attrs: AttributeSet ?= null
) : LinearLayout(context,attrs){
    private var mIconModels: List<IconModel> = emptyList()
    private var currentSeledtedIconType: OperationType ?= null

    fun setIconModels(iconModels: List<IconModel>){
        mIconModels = iconModels
        val defaultWidth = dp2px(50)
        val defaultHeight = dp2px(50)
        val lp = LayoutParams(defaultWidth,defaultHeight)
        lp.weight = 1f
        if(orientation == VERTICAL){
            lp.topMargin = dp2px(10)
        }else{
            lp.rightMargin = dp2px(10)
        }
        weightSum = iconModels.size.toFloat()
        iconModels.forEach {model ->
            val circleIconTextView: CircleIconTextView = CircleIconTextView(context)
            circleIconTextView.setIconModel(model)
            addView(circleIconTextView,lp)
        }
    }
    fun cancelSelectedIcon(type: OperationType?){
        if(currentSeledtedIconType == null || type == null)return
        if(type != currentSeledtedIconType){
            val currentTypeIndex = currentSeledtedIconType!!.index
            val view = getChildAt(currentTypeIndex) as CircleIconTextView
            view.cancelSelectedColor()
        }
    }
    fun changeCurrentSelectedIcon(type: OperationType?){
        cancelSelectedIcon(type)
        currentSeledtedIconType = type
    }
    fun cancelSelect(type: OperationType?){
        val currentTypeIndex = type!!.index
        val view = getChildAt(currentTypeIndex) as CircleIconTextView
        view.cancelSelectedColor()
    }

}